package aos.ydksxt.service.jcxxcj;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjFcjcxx;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 房产基础信息采集服务
 * Created by Stiofan on 2016/12/16.
 */
@Service
public class FcjcxxcjService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("/ydksxt/jcxxcj/fcjcxxcj.jsp");
    }
    @Transactional
    public void queryFcjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("fcbh", inDto.get("fcbh"));
        param.put("fczh", inDto.get("fczh"));
        DjFcjcxx djFcjcxx = (DjFcjcxx) persistenceDAO.findSingleByHql("from DjFcjcxx xx where xx.yxbz='Y' and (xx.fcbh=:fcbh or xx.fczh=:fczh)", param);
        Dto outDto = Dtos.newOutDto();
        if (ObjectUtils.anyNotNull(djFcjcxx)) {
            if (!httpModel.getUserModel().getAccount().equals(djFcjcxx.getSsglyDm()) && djFcjcxx.getSsglyDm() != null) {//判断税收管理员代码与当前操作人员不一致，给我提示
                checkSsgly(outDto, djFcjcxx.getSsglyDm());//校验税收管理员
            }
        } else {
            outDto.setAppCode("noFcjcxx");
        }
        outDto.putAll((Map) AOSUtils.object2Map(djFcjcxx));
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void queryFcjcxxByFcbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("fcbh", inDto.get("fcbh"));
        DjFcjcxx djFcjcxx = (DjFcjcxx) persistenceDAO.findSingleByHql("from DjFcjcxx xx where xx.fcbh=:fcbh and xx.yxbz='Y'", params);
        Dto outDto = Dtos.newOutDto();
        if (djFcjcxx != null) {
            outDto.setAppCode("suc");
            outDto.putAll((Map) AOSUtils.object2Map(djFcjcxx));
            outDto.put("zldz", getFullAddress(djFcjcxx.getXzqhdsDm(), djFcjcxx.getXzqhxqDm(), djFcjcxx.getJdxzDm(), djFcjcxx.getZldzSqjqDm(), djFcjcxx.getZldzDlxdDm(), djFcjcxx.getZldzLh()));
        } else {
            outDto.setAppCode("err");
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void queryAllFcjcxxByTdbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("tdbh", inDto.getString("tdbh"));
        List fcjcxxes = persistenceDAO.findAllByHql("from DjFcjcxx xx where xx.tdbh=:tdbh and xx.yxbz='Y'", params);
        httpModel.setOutMsg(AOSJson.toGridJson(fcjcxxes, inDto.getPageTotal()));
    }
    @Transactional
    public void saveFcjcxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("fcbh", inDto.get("fcbh"));
        DjFcjcxx djFcjcxx = (DjFcjcxx) persistenceDAO.findSingleByHql("from DjFcjcxx xx where xx.fcbh=:fcbh", param);
        if (djFcjcxx == null) {
            djFcjcxx = new DjFcjcxx();
        }
        AOSUtils.copyProperties(inDto, djFcjcxx);
        djFcjcxx.setYxbz("Y");
        djFcjcxx.setSsglyDm(getCzryDm(httpModel));
        djFcjcxx.setSjgsqs(getCzjgDm(httpModel));
        if (StringUtils.isEmpty(djFcjcxx.getFcbh())) {
            djFcjcxx.setFcbh(getSybh(djFcjcxx.getXzqhxqDm()));
            djFcjcxx.setLrrDm(getCzryDm(httpModel));
            djFcjcxx.setLrrq(getDateTime());
        } else {
            djFcjcxx.setXgrDm(getCzryDm(httpModel));
            djFcjcxx.setXgrq(getDateTime());
        }
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djFcjcxx);
            outDto.setAppCode("suc");
            outDto.putAll((Map) AOSUtils.object2Map(djFcjcxx));
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void zfFcjcxxByFcbh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("fcbh", inDto.get("fcbh"));
        DjFcjcxx djFcjcxx = (DjFcjcxx) persistenceDAO.findSingleByHql("from DjFcjcxx xx where xx.fcbh=:fcbh", param);
        djFcjcxx.setXgrDm(getCzryDm(httpModel));
        djFcjcxx.setXgrq(getDateTime());
        djFcjcxx.setYxbz("N");
        djFcjcxx.setZfrDm(getCzryDm(httpModel));
        djFcjcxx.setZfrq(getDateTime());
        djFcjcxx.setZfyy(inDto.getString("zfyy"));
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djFcjcxx);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
}
